package com.weiyi.mashangzhu.Roomer.Activity;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Adapter.AllCommentAdapter;
import com.weiyi.mashangzhu.Roomer.Bean.AllCommentInfo;
import com.weiyi.mashangzhu.Utils.ListScrollUtil;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AllCommentActivity extends BaseActivity implements
        PullToRefreshBase.OnRefreshListener2<ScrollView> {


    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.pull_refresh_scrollview)
    PullToRefreshScrollView pullRefreshScrollview;
    @BindView(R.id.layout)
    LinearLayout layout;
    List<AllCommentInfo> list = new ArrayList<>();
    AllCommentAdapter allcommentadapter = null;
    String id;
    int page = 1;
    String type;
    String url;
    String id_name;
    String baseurl;
    List<AllCommentInfo.DataBean.EvalistBean> evalistBeanList = new ArrayList<AllCommentInfo.DataBean.EvalistBean>();
    @BindView(R.id.good)
    TextView good;
    @BindView(R.id.size_comment)
    TextView sizeComment;
    @BindView(R.id.weisheng)
    TextView weisheng;
    @BindView(R.id.fuwu)
    TextView fuwu;
    @BindView(R.id.jiaotong)
    TextView jiaotong;
    @BindView(R.id.xingjiabi)
    TextView xingjiabi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_comment);
        ButterKnife.bind(this);
        id = getIntent().getStringExtra("id");
        type=getIntent().getStringExtra("type");
        if (type.equals("house")){
            id_name="id";
            url=API.MoreComment_House_Api;
            baseurl=API.BaseApi_R;
        }else if(type.equals("host")){
            id_name="sellerId";
            url=API.MoreComment_Api;
            baseurl=API.BaseApi_R;
        }else if(type.equals("house_l")){
            id_name="id";
            url=API.MoreComment_House_Api;
            baseurl=API.BaseApi_L;
        }

//        T.showShort(this, id+"idname="+id_name);
        initView();
    }

    @Override
    public void initView() {
        super.initView();

        //设置当前焦点，防止打开Activity出现在ListView位置问题
        layout.setFocusable(true);
        layout.setFocusableInTouchMode(true);
        layout.requestFocus();
        // 刷新label的设置
        pullRefreshScrollview.getLoadingLayoutProxy().setLastUpdatedLabel(
                "上次刷新时间");
        pullRefreshScrollview.getLoadingLayoutProxy()
                .setPullLabel("下拉刷新");
//          mPullRefreshScrollView.getLoadingLayoutProxy().setRefreshingLabel(
//                      "refreshingLabel");
        pullRefreshScrollview.getLoadingLayoutProxy().setReleaseLabel(
                "松开即可刷新");
        // 上拉、下拉设定
        pullRefreshScrollview.setMode(PullToRefreshBase.Mode.BOTH);
        pullRefreshScrollview.setOnRefreshListener(this);
        getData(page);


    }

    //初始化评价列表
    private void getData(final int p) {
        OkHttpUtils.get().url(baseurl + url).addParams("token", SPUtils.get(this, "token", "") + "").addParams(id_name, id).addParams("pagenum", p + "").build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                T.showShort(AllCommentActivity.this, "网络连接失败，请检查一下网络设置");
            }

            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                AllCommentInfo allCommentInfo = gson.fromJson(response, AllCommentInfo.class);
                good.setText(allCommentInfo.getData().getPositiverat1()+"");
                sizeComment.setText(allCommentInfo.getData().getEvalist().size()+"评价");
                weisheng.setText(allCommentInfo.getData().getEHealthmark()+"");
                fuwu.setText(allCommentInfo.getData().getEServicemark()+"");
                jiaotong.setText(allCommentInfo.getData().getETrafficmark()+"");
                xingjiabi.setText(allCommentInfo.getData().getECountsmark()+"");

                if (p == 1) {
                    evalistBeanList.clear();
                    evalistBeanList = allCommentInfo.getData().getEvalist();
                    allcommentadapter = new AllCommentAdapter(evalistBeanList, AllCommentActivity.this);
                    listview.setAdapter(allcommentadapter);
                } else {
                    evalistBeanList.addAll(allCommentInfo.getData().getEvalist());
                    allcommentadapter.notifyDataSetChanged();
                }


            }
        });
    }

    //上拉
    private void task1() {
        page += 1;
        getData(page);
        //将ListView高度设置成ScrollView高度。每次数据改变都要刷新高度
        ListScrollUtil.setListViewHeightBasedOnChildren(listview);
        pullRefreshScrollview.onRefreshComplete();
    }

    //下拉
    private void task2() {
        page = 1;
        getData(page);
        //将ListView高度设置成ScrollView高度。每次数据改变都要刷新高度
        ListScrollUtil.setListViewHeightBasedOnChildren(listview);
        pullRefreshScrollview.onRefreshComplete();
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
        task2();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
        task1();
    }
}
